package org.zxy.auth.controller;

import org.zxy.auth.dto.RoleDto;
import org.zxy.auth.service.IRoleService;
import org.zxy.auth.domain.Role;
import org.zxy.auth.query.RoleQuery;
import org.zxy.base.annotation.RonghuaPermission;
import org.zxy.base.util.AjaxResult;
import org.zxy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    public IRoleService roleService;


    // /**
    //  * 保存和修改
    //  *
    //  * @param role 传递的实体
    //  * @return Ajaxresult转换结果
    //  */
    // @PutMapping
    // public AjaxResult insertOrUpdate(@RequestBody Role role) {
    //     try {
    //         if ( role.getId() != null)
    //             roleService.update(role);
    //         else
    //             roleService.insert(role);
    //         return AjaxResult.success();
    //     } catch (Exception e) {
    //         e.printStackTrace();
    //         return AjaxResult.error("保存对象失败！" + e.getMessage());
    //     }
    // }
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody RoleDto roleDto) {
        try {
            roleService.saveOrUpdate(roleDto);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存或更新对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            roleService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 根据Id查询对象信息
     *
     * @param
     * @return
     */
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        try {
            Role role = roleService.selectById(id);
            return AjaxResult.success(role);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有对象信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult selectAll() {

        try {
            List<Role> list = roleService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult queryPage(@RequestBody RoleQuery query) {
        try {
            PageList<Role> pageList = roleService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除对象
     *
     * @param
     * @return
     */
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            roleService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }

    @GetMapping("/permsssion/{id}")
    @RonghuaPermission(name = "角色查询权限")
    public AjaxResult getPermsssionId(@PathVariable("id") Long id) {
        try {
            List<Long> ids = roleService.getPermsssionId(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
}
